package com.crm.crm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.crm.domain.CluePool;
import com.crm.crm.domain.InsertRecord;
import com.crm.crm.domain.SysUser;
import com.crm.crm.domain.dto.UserPageQueryParam;
import com.crm.crm.domain.vo.PageResult;
import com.crm.crm.domain.vo.UserVo;
import com.crm.crm.domain.vo.business.result.HttpStatus;
import com.crm.crm.domain.vo.business.result.TableDataInfo;
import com.crm.crm.domain.vo.login.TransferVo;
import com.crm.crm.mapper.BusinessMapper;
import com.crm.crm.mapper.ClueMapper;
import com.crm.crm.mapper.TransferMapper;
import com.crm.crm.service.ClueService;
import com.crm.crm.service.TransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TransferServiceImpl extends ServiceImpl<TransferMapper, InsertRecord> implements TransferService {
    @Autowired
    private TransferMapper transferMapper;

    @Autowired
    private ClueMapper clueMapper;

    @Autowired
    private BusinessMapper  businessMapper;
    @Override
    public TableDataInfo<TransferVo> listInfo(UserPageQueryParam userPageQueryParam) {
        Page<InsertRecord> page = new Page<>(userPageQueryParam.getPageNum(), userPageQueryParam.getPageSize());
        LambdaQueryWrapper<InsertRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotEmpty(userPageQueryParam.getUserName()), InsertRecord::getCreateBy, userPageQueryParam.getUserName())
                .like(StrUtil.isNotEmpty(userPageQueryParam.getPhonenumber()), InsertRecord::getPhonenumber, userPageQueryParam.getPhonenumber());
        if (StringUtils.isNotEmpty(userPageQueryParam.getBeginTime()) && StringUtils.isNotEmpty(userPageQueryParam.getEndTime())) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate beginCreatTime = LocalDate.parse(userPageQueryParam.getBeginTime(), formatter);
            LocalDate endCreatTime = LocalDate.parse(userPageQueryParam.getEndTime(), formatter);
            wrapper.between(InsertRecord::getCreateTime, beginCreatTime, endCreatTime);
        }
        Page<InsertRecord> insertRecordPage = transferMapper.selectPage(page, wrapper);
        List<InsertRecord> records = page.getRecords();
        List<TransferVo> collect = records.stream().map(insertRecord -> {
            TransferVo transferVo = new TransferVo();
            BeanUtil.copyProperties(insertRecord, transferVo);
            transferVo.setUserName(insertRecord.getCreateBy());
            transferVo.setClueNum(insertRecord.getClueCount());
            transferVo.setBusinessNum(insertRecord.getBusinessCount());
            return transferVo;
        }).collect(Collectors.toList());
        TableDataInfo<TransferVo> data = new TableDataInfo<>();
        data.setTotal(insertRecordPage.getTotal());
        data.setRows(collect);
        data.setCode(HttpStatus.SUCCESS);
        data.setMsg("查询成功");


        return data;
    }

    //ex是insert—_record里的userid
    // type是0代表线索，1代表商机
    //  finalEx是sys_user里的userid
    @Override
    public void assignment(Integer type, Long ex, Long finalEx) {
        if (type == 0) {
            //把线索表里的owner改为 finalEx所对应的名字
            String name = transferMapper.selcetByUserId(finalEx);
            List<Integer> ids = clueMapper.selectIds(name);
            clueMapper.setOwner(ids, name);
            //把insert_record里的finalEx对应的clue_count设置为ex的clue_count
            Integer clueCount = transferMapper.getClueCount(ex);
            Integer clueCount1 =  transferMapper.getClueCount(finalEx);
            Integer clueCount2 = clueCount1 + clueCount;
            transferMapper.setClueCount(finalEx, clueCount2);
            //再把insert_record里的ex对应的clue_count清零
            transferMapper.cleanClueCount(ex);
        } else if (type == 1) {
            //把商机表里的owner改为 finalEx所对应的名字
            String name = transferMapper.selcetByUserId(finalEx);
            List<Integer> ids = businessMapper.selectIds(name);
            businessMapper.setOwner(ids, name);
            //把insert_record里的finalEx对应的business_count设置为ex的business_count
            Integer businessCount = transferMapper.getBusinessCount(ex);
            Integer businessCount1 =  transferMapper.getBusinessCount(finalEx);
            Integer businessCount2 = businessCount1 + businessCount;
            transferMapper.setBusinessCount(finalEx, businessCount2);
            //再把insert_record里的ex对应的business_count清零
            transferMapper.cleanBusinessCount(ex);
        }
    }
}
